package com.vm.action.mobile;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Timestamp;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Preparable;
import com.vm.action.ActionSuper;
import com.vm.common.Page;
import com.vm.dao.mobile.base.IAppDetailDao;
import com.vm.dao.mobile.base.IMobileAppManageDao;
import com.vm.entity.AppDetail;
import com.vm.log4j.CustomizeLog4j;

public class AppDetailAction extends ActionSuper implements Preparable {
	
	private static Logger log = CustomizeLog4j.getLogger(AppDetailAction.class);
	
	private Page<AppDetail> page;
	private AppDetail appDetail;
	private IAppDetailDao appDetailDao;
	private IMobileAppManageDao mobileAppManageDao;
	private Map<String,String> sectionMap;
	private String pageLinkList;
	private Integer sectionId;
	private Integer id;
	
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	private String savePath;
	
	
	public void prepare() throws Exception {
		//sectionMap = this.mobileAppManageDao.getSelectPoMap("select bkId as id,bkName as name from Section");
		//atuo
	}
	
	public String list(){
		this.page = this.appDetailDao.getPageBySectionId(sectionId,this.PAGE_ROW_SIZE, super.getPageNumber());
		this.pageLinkList =super.getSplitPageLink("/mobile/AppDetailAction_list.do?", this.page);
		
		return SUCCESS;
	}
	
	public String add() throws Exception {
		if(this.appDetail != null){
			appDetail = null;
		}
		return SUCCESS;
	}

	public String save() throws Exception {
		String[] msgArg = new String[] { "AppDetailAction_list.do"};
		String msg = null;
		try {
			String path = this.saveFile();
			appDetail.setAlUrl(path);
			
			Timestamp time = new Timestamp(System.currentTimeMillis());
			appDetail.setAlScsj(time);
			appDetailDao.save(appDetail);
			msg = super.getResourceMessage("insert_appDetail_success", msgArg);
		} catch (Exception e) {
			log.error("AppDetailAction save appDetail error  ", e);
			msg = super.getResourceMessage("insert_appDetail_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}

	public String edit(){
		appDetail = appDetailDao.findById(this.getId());
		return SUCCESS;
	}

	public String update() throws Exception {
		String[] msgArg = new String[] { "AppDetailAction_list.do"};
		String msg = null;
		try {
			Timestamp time = new Timestamp(System.currentTimeMillis());
			appDetail.setAlScsj(time);
			String path = this.saveFile();
			appDetail.setAlUrl(path);
			appDetailDao.update(appDetail);
			msg = super.getResourceMessage("update_appDetail_success", msgArg);
		} catch (Exception e) {
			log.error("AppDetailAction save appDetail error  ", e);
			msg = super.getResourceMessage("update_appDetail_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;

	}

	public String delete() {
		String[] msgArg = new String[] { "AppDetailAction_list.do"};
		String msg = null;
		try {
			appDetailDao.deleteById(this.getId());
			msg = super.getResourceMessage("delete_appDetail_success", msgArg);
		} catch (Exception e) {
			log.error("AppDetailAction delete appDetail error  ", e);
			msg = super.getResourceMessage("delete_appDetail_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}

	public String saveFile() throws Exception{
		String path = "";
		if(this.getUploadContentType() == null){
			return path;
		}
		String fileName = getSavePath() + "\\" +getUploadFileName();
		FileOutputStream fos = new FileOutputStream(fileName);
		FileInputStream fis = new FileInputStream(this.getUpload());
		byte[] b = new byte[1024];
		int len = 0;
		while((len = fis.read(b)) > 0){
			fos.write(b, 0, len);
		}
		path = fileName;
		return path;
		
	}
	
	public Page<AppDetail> getPage() {
		return page;
	}

	public void setPage(Page<AppDetail> page) {
		this.page = page;
	}

	public AppDetail getAppDetail() {
		return appDetail;
	}

	public void setAppDetail(AppDetail appDetail) {
		this.appDetail = appDetail;
	}

	public IAppDetailDao getAppDetailDao() {
		return appDetailDao;
	}

	public void setAppDetailDao(IAppDetailDao appDetailDao) {
		this.appDetailDao = appDetailDao;
	}

	public String getPageLinkList() {
		return pageLinkList;
	}

	public void setPageLinkList(String pageLinkList) {
		this.pageLinkList = pageLinkList;
	}

	public IMobileAppManageDao getMobileAppManageDao() {
		return mobileAppManageDao;
	}

	public void setMobileAppManageDao(IMobileAppManageDao mobileAppManageDao) {
		this.mobileAppManageDao = mobileAppManageDao;
	}

	public Map<String, String> getSectionMap() {
		return sectionMap;
	}

	public void setSectionMap(Map<String, String> sectionMap) {
		this.sectionMap = sectionMap;
	}

	public Integer getSectionId() {
		return sectionId;
	}

	public void setSectionId(Integer sectionId) {
		this.sectionId = sectionId;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getSavePath() {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	
	
}
